<!--用户注册页面-->
<template>
  <div>
    <div style="box-shadow: 0 0 5px #999;width: 800px;margin: 100px auto;background-color: white;padding: 20px 0">
      <h2>企业注册</h2>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <div style="text-align: center;">
          <el-form-item label="用户名" style="padding: 20px;size: 27px" prop="username">
            <el-input
                placeholder="请输入用户名称"
                prefix-icon="el-icon-user-solid"
                v-model="form.username"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-form-item label="企业全称" style="padding: 20px;size: 27px" prop="companyname">
            <el-input
                placeholder="请输入企业全称"
                prefix-icon="el-icon-mobile-phone"
                v-model="form.companyname"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-form-item label="主营项目" style="padding: 20px;size: 27px" prop="mainproject">
            <el-input
                placeholder="请输入主营项目"
                prefix-icon="el-icon-mobile-phone"
                v-model="form.mainproject"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-form-item label="企业电话" style="padding: 20px;size: 27px" prop="companyphone">
            <el-input
                placeholder="请输入企业电话号"
                prefix-icon="el-icon-phone"
                v-model="form.companyphone"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>

        <div style="text-align: center;">
          <el-form-item label="联系人" style="padding: 20px;size: 27px" prop="contactperson">
            <el-input
                placeholder="请输入联系人"
                prefix-icon="el-icon-user-solid"
                v-model="form.contactperson"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-form-item label="密 码" style="padding: 20px;size: 27px" prop="password">
            <el-input
                placeholder="设置6-20位登录密码"
                prefix-icon="el-icon-lock"
                v-model="form.password"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-form-item label="确认密码" style="padding: 20px;size: 27px" prop="confirmpassword">
            <el-input
                placeholder="设置6-20位登录密码"
                prefix-icon="el-icon-lock"
                v-model="form.confirmpassword"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-form-item label="联系人电话" style="padding: 20px;size: 27px" prop="contactphone">
            <el-input
                placeholder="请输入联系人电话号"
                prefix-icon="el-icon-phone"
                v-model="form.contactphone"
                style="display: inline-block;width: 397px;">
            </el-input>
          </el-form-item>
        </div>
        <div style="text-align: center;">
          <el-row>
            <el-form-item>
              <el-button type="primary" @click="submitForm('form')">注册</el-button>
              <el-button @click="resetForm('form')">重置</el-button>
            </el-form-item>
          </el-row>
        </div>
      </el-form>
    </div>

  </div>
</template>

<style>
h2 {
  text-align: center;
}

</style>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        confirmpassword:'',
        companyname: '',
        companyphone: '',
        contactperson: '',
        contactphone: '',
        mainproject: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 25 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        companyphone: [
          {required: true, message: '请输入企业电话', trigger: 'blur'},
          {min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur'}
        ],
        companyname: [
          {required: true, message: '请输入企业名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        contactperson: [
          {required: true, message: '请输入联系人', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        contactphone: [
          {required: true, message: '请输入联系人电话', trigger: 'blur'},
          {min: 8, max: 35, message: '长度在 8 到 35 个字符', trigger: 'blur'}
        ],
        mainproject: [
          {required: true, message: '请输入主要项目', trigger: 'blur'},
        ]
      }
    };
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/users/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.form);
          console.log('formData：' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url).then((response)=> {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加用户成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

}

</script>